package org.mspring.mlog.action.blog;

import org.mspring.mlog.ArticleViewCountService;
import org.mspring.mlog.BlogUtils;
import org.mspring.mlog.ServiceContext;
import org.mspring.mlog.common.ArticleDto;
import org.mspring.mlog.entity.Article;
import org.mspring.mlog.entity.Category;
import org.mspring.mlog.entity.em.ArticleStatus;
import org.mspring.mlog.service.ArticleService;
import org.mspring.mlog.service.CategoryService;
import org.mspring.mlog.service.TagService;
import org.mspring.nbee.db.dao.sql.builder.part.condition.Operator;
import org.mspring.nbee.db.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.db.dao.sql.builder.part.condition.impl.SimpleCondition;
import org.mspring.nbee.db.dao.sql.pager.OrderType;
import org.mspring.nbee.db.dao.sql.pager.PageResult;
import org.mspring.nbee.db.dao.sql.pager.Pager;
import org.mspring.nbee.web.Action;
import org.mspring.nbee.web.support.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2015-01-11 12:43
 */
@Action("blogArticleAction")
public class ArticleAction {
    private static final Logger LOG = LoggerFactory.getLogger(ArticleAction.class);

    private ArticleService articleService;
    private CategoryService categoryService;
    private TagService tagService;
    private ArticleViewCountService articleViewCountService;
    private ServiceContext serviceContext;

    @RequestMapping("/article/{id:\\d+}")
    public String view(@PathVariable long id, WebContext context, Model model) throws IOException {
        if (id <= 0) {
            context.getResponse().sendError(404);
        }
        Article article = articleService.findByIdWithCache(id);
        if (article == null || article.getStatus() != ArticleStatus.NORMAL) {
            context.getResponse().sendError(404);
        }
        model.addAttribute("article", ArticleDto.fromArticle(article, serviceContext));
        articleViewCountService.increment(article.getId());
        BlogUtils.setSiteTitle(article.getTitle(), model);
        return "theme:/article";
    }

    /**
     * 根据分类归档
     *
     * @param categoryId
     * @param context
     * @param model
     * @return
     */
    @RequestMapping("/article/category/{categoryId:\\d+}")
    public String categoryArchivers(@PathVariable long categoryId, WebContext context, Model model) throws IOException {
        Category category = categoryService.findById(categoryId);
        if (category == null) {
            context.getResponse().sendError(404);
        }
        Pager pager = context.getPager();
        pager.addOrder("id", OrderType.DESC);
        List<SqlCondition> conditions = new ArrayList<>();
        conditions.add(new SimpleCondition("category", Operator.EQ, categoryId));
        conditions.add(new SimpleCondition("status", Operator.EQ, ArticleStatus.NORMAL));
        PageResult<Article> tempResult = articleService.list(conditions, pager);
        PageResult<ArticleDto> articleResult = new PageResult<>();
        articleResult.setPager(tempResult.getPager());
        articleResult.setTotal(tempResult.getTotal());
        articleResult.setData(ArticleDto.fromArticles(tempResult.getData(), serviceContext));
        model.addAttribute("articleResult", articleResult);
        model.addAttribute("category", categoryId);
        BlogUtils.setSiteTitle("分类 - " + category.getName(), model);
        return "theme:/index";
    }

    /**
     * 根据标签归档
     *
     * @param tagId
     * @param context
     * @param model
     * @return
     */
    @RequestMapping("/article/tag/{tagId:\\d+}")
    public String tagArchivers(@PathVariable long tagId, WebContext context, Model model) {
        return "theme:/index";
    }

    @Autowired
    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Autowired
    public void setTagService(TagService tagService) {
        this.tagService = tagService;
    }

    @Autowired
    public void setArticleViewCountService(ArticleViewCountService articleViewCountService) {
        this.articleViewCountService = articleViewCountService;
    }

    @Autowired
    public void setServiceContext(ServiceContext serviceContext) {
        this.serviceContext = serviceContext;
    }
}
